home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / components / sbISimplePlaylist.js < prev    next >
Encoding:
Text File  |  2006-02-07  |  17.2 KB  |  657 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbIPlaylist Object
  29. //
  30.  
  31. const SONGBIRD_SIMPLEPLAYLIST_CONTRACTID = "@songbird.org/Songbird/SimplePlaylist;1";
  32. const SONGBIRD_SIMPLEPLAYLIST_CLASSNAME = "Songbird SimplePlaylist Interface"
  33. const SONGBIRD_SIMPLEPLAYLIST_CID = Components.ID('{7B2945F6-6A00-4489-AD2F-95BA25F4D1EA}');
  34. const SONGBIRD_SIMPLEPLAYLIST_IID = Components.interfaces.sbISimplePlaylist;
  35.  
  36. const SIMPLEPLAYLIST_LIST_TABLE_NAME = "simpleplaylist_list";
  37.  
  38. function CSimplePlaylist()
  39. {
  40. }
  41.  
  42. /* the CPlaylist class def */
  43. CSimplePlaylist.prototype = 
  44. {
  45.   m_strName: "",
  46.   m_strReadableName: "",
  47.   m_queryObject: null,
  48.   
  49.   SetQueryObject: function(queryObj)
  50.   {
  51.     this.m_queryObject = queryObj; 
  52.     return;
  53.   },
  54.   
  55.   GetQueryObject: function()
  56.   {
  57.     return this.m_queryObject;
  58.   },
  59.   
  60.   AddByURL: function(strURL, nColumnCount, aColumns, nValueCount, aValues, bWillRunLater)
  61.   {
  62.     if(this.m_queryObject != null)
  63.     {
  64.     
  65.       var i = 0;
  66.       var strQuery = "INSERT INTO \"" + this.m_strName + "\" (url, ";
  67.       
  68.       for(; i < nColumnCount; i++)
  69.       {
  70.         strQuery += aColumns[i];
  71.  
  72.         if(i != nColumnCount - 1)
  73.           strQuery += ", ";
  74.       }
  75.  
  76.       strQuery += ") VALUES (\"" + strURL + "\", ";
  77.  
  78.       for(i = 0; i < nValueCount; i++)
  79.       {
  80.         strQuery += "\"" + aValues[i] + "\"";
  81.  
  82.         if(i != nValueCount - 1)
  83.           strQuery += ", ";
  84.       }
  85.  
  86.       strQuery += ")";
  87.  
  88.       if(!bWillRunLater)
  89.         this.m_queryObject.ResetQuery();
  90.         
  91.       this.m_queryObject.AddQuery(strQuery);
  92.  
  93.       if(!bWillRunLater)
  94.       {
  95.         this.m_queryObject.Execute();
  96.         this.m_queryObject.WaitForCompletion();
  97.       }
  98.     }
  99.     
  100.     return;
  101.   },
  102.  
  103.   RemoveByURL: function(strURL, bWillRunLater)
  104.   {
  105.     if(this.m_queryObject != null)
  106.     {
  107.       if(!bWillRunLater)
  108.         this.m_queryObject.ResetQuery();
  109.  
  110.       this.m_queryObject.AddQuery("DELETE FROM \"" + this.m_strName + "\" WHERE url = \"" + strURL + "\"");
  111.       
  112.       if(!bWillRunLater)
  113.       {
  114.         this.m_queryObject.Execute();
  115.         this.m_queryObject.WaitForCompletion();
  116.       }
  117.       
  118.       return true;
  119.     }
  120.     
  121.     return false;
  122.   },
  123.   
  124.   RemoveByIndex: function(nIndex, bWillRunLater)
  125.   {
  126.     if(this.m_queryObject != null)
  127.     {
  128.       if(!bWillRunLater)
  129.         this.m_queryObject.ResetQuery();
  130.  
  131.       this.m_queryObject.AddQuery("DELETE FROM \"" + this.m_strName + "\" WHERE id = \"" + nIndex + "\"");
  132.       
  133.       if(!bWillRunLater)
  134.       {
  135.         this.m_queryObject.Execute();
  136.         this.m_queryObject.WaitForCompletion();
  137.       }
  138.       
  139.       return true;
  140.     }
  141.     
  142.     return false;  
  143.   },
  144.  
  145.   RemoveByGUID: function(mediaGUID, bWillRunLater)
  146.   {
  147.     if(this.m_queryObject != null)
  148.     {
  149.       if(!bWillRunLater)
  150.         this.m_queryObject.ResetQuery();
  151.  
  152.       this.m_queryObject.AddQuery("DELETE FROM \"" + this.m_strName + "\" WHERE playlist_uuid = \"" + mediaGUID + "\"");
  153.       
  154.       if(!bWillRunLater)
  155.       {
  156.         this.m_queryObject.Execute();
  157.         this.m_queryObject.WaitForCompletion();
  158.       }
  159.       
  160.       return true;
  161.     }
  162.     
  163.     return false;
  164.   },
  165.   
  166.   FindByURL: function(strURL)
  167.   {
  168.     if(this.m_queryObject != null)
  169.     {
  170.       this.m_queryObject.ResetQuery();
  171.       this.m_queryObject.AddQuery("SELECT id FROM \"" + this.m_strName + "\" WHERE url = \"" + strURL + "\"");
  172.       
  173.       this.m_queryObject.Execute();
  174.       this.m_queryObject.WaitForCompletion();
  175.       
  176.       var resObj = this.m_queryObject.GetResultObject();
  177.       return resObj.GetRowCell(0, 0);
  178.     }
  179.     
  180.     return -1;
  181.   },
  182.   
  183.   FindByIndex: function(nIndex)
  184.   {
  185.     if(this.m_queryObject != null)
  186.     {
  187.       this.m_queryObject.ResetQuery();
  188.       this.m_queryObject.AddQuery("SELECT url FROM \"" + this.m_strName + "\" WHERE id = \"" + nIndex + "\"");
  189.       
  190.       this.m_queryObject.Execute();
  191.       this.m_queryObject.WaitForCompletion();
  192.       
  193.       var resObj = this.m_queryObject.GetResultObject();
  194.       return resObj.GetRowCell(0, 0);
  195.     }
  196.     
  197.     return "";
  198.   },
  199.  
  200.   GetColumnInfo: function()
  201.   {
  202.     if(this.m_queryObject != null)
  203.     {
  204.       this.m_queryObject.ResetQuery();
  205.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "_desc\"");
  206.       
  207.       this.m_queryObject.Execute();
  208.       this.m_queryObject.WaitForCompletion();
  209.     }
  210.   },
  211.   
  212.   SetColumnInfo: function(strColumn, strReadableName, isVisible, defaultVisibility, isMetadata, sortWeight, colWidth, bWillRunLater)
  213.   {
  214.     if(this.m_queryObject != null)
  215.     {
  216.       if(!bWillRunLater)
  217.         this.m_queryObject.ResetQuery();
  218.       
  219.       var strQuery = "UPDATE \"" + this.m_strName + "_desc\" SET ";
  220.       strQuery += "readable_name = \"" + strReadableName + "\", ";
  221.       strQuery += "is_visible = \"" + (isVisible == true ? "1" : "0") + "\", ";
  222.       strQuery += "default_visibility = \"" + (defaultVisibility == true ? "1" : "0") + "\", ";
  223.       strQuery += "is_metadata = \"" + (isMetadata == true ? "1" : "0") + "\", ";
  224.       strQuery += "sort_weight = \"" + sortWeight + "\", ";
  225.       strQuery += "width = \"" + colWidth + "\" ";
  226.       strQuery += "WHERE column_name = \"" + strColumn + "\"";
  227.       
  228.       this.m_queryObject.AddQuery(strQuery);
  229.       
  230.       if(!bWillRunLater)
  231.       {
  232.         this.m_queryObject.Execute();
  233.         this.m_queryObject.WaitForCompletion();
  234.       }
  235.     }    
  236.   },
  237.   
  238.   GetTableInfo: function()
  239.   {
  240.     if(this.m_queryObject != null)
  241.     {
  242.       this.m_queryObject.ResetQuery();
  243.       this.m_queryObject.AddQuery("PRAGMA table_info(\"" + this.m_strName + "\")");
  244.       
  245.       this.m_queryObject.Execute();
  246.       this.m_queryObject.WaitForCompletion();
  247.     }    
  248.     
  249.     return;
  250.   },
  251.   
  252.   AddColumn: function(strColumn, strType)
  253.   {
  254.     if(this.m_queryObject != null)
  255.     {
  256.       this.m_queryObject.ResetQuery();
  257.       
  258.       this.m_queryObject.AddQuery("ALTER TABLE \"" + this.m_strName + "\" ADD COLUMN \"" + strColumn + "\" " + strType);
  259.       this.m_queryObject.AddQuery("INSERT OR REPLACE INTO \"" + this.m_strName + "_desc\" (column_name) VALUES (\"" + strColumn + "\")");
  260.       
  261.       this.m_queryObject.Execute();
  262.       this.m_queryObject.WaitForCompletion();
  263.     }
  264.  
  265.     return;
  266.   },
  267.   
  268.   DeleteColumn: function(strColumn)
  269.   {
  270.     if(this.m_queryObject != null)
  271.     {
  272.     }
  273.  
  274.     return;
  275.   },
  276.  
  277.   GetNumEntries: function()
  278.   {
  279.     if(this.m_queryObject != null)
  280.     {
  281.       this.m_queryObject.ResetQuery();
  282.       this.m_queryObject.AddQuery("SELECT COUNT(id) FROM \"" + this.m_strName + "\"");
  283.       
  284.       this.m_queryObject.Execute();
  285.       this.m_queryObject.WaitForCompletion();
  286.       
  287.       var resObj = this.m_queryObject.GetResultObject();
  288.       
  289.       return resObj.GetRowCell(0, 0);
  290.     }
  291.     
  292.     return 0;
  293.   },  
  294.   
  295.   GetEntry: function(nEntry)
  296.   {
  297.     if(this.m_queryObject != null)
  298.     {
  299.       this.m_queryObject.ResetQuery();
  300.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "\" WHERE id = \"" + nEntry + "\"");
  301.       
  302.       this.m_queryObject.Execute();
  303.       this.m_queryObject.WaitForCompletion();
  304.  
  305.       return 1;
  306.     }
  307.     
  308.     return 0;
  309.   },
  310.  
  311.   GetAllEntries: function()
  312.   {
  313.     if(this.m_queryObject != null)
  314.     {
  315.       this.m_queryObject.ResetQuery();
  316.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "\"");
  317.       
  318.       this.m_queryObject.Execute();
  319.       this.m_queryObject.WaitForCompletion();
  320.       
  321.       var resObj = this.m_queryObject.GetResultObject();
  322.       return resObj.GetRowCount();
  323.     }
  324.     
  325.     return 0;
  326.   },
  327.  
  328.   GetColumnValueByIndex: function(mediaIndex, strColumn)
  329.   {
  330.     if(this.m_queryObject != null)
  331.     {
  332.       this.m_queryObject.ResetQuery();
  333.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + this.m_strName + "\" WHERE id = \"" + mediaIndex + "\"");
  334.       
  335.       this.m_queryObject.Execute();
  336.       this.m_queryObject.WaitForCompletion();
  337.       
  338.       var resObj = this.m_queryObject.GetResultObject();
  339.  
  340.       if(resObj.GetRowCount())
  341.         return resObj.GetRowCell(0, 0);
  342.     }
  343.  
  344.     return "";
  345.   },
  346.   
  347.   GetColumnValueByURL: function(mediaURL, strColumn)
  348.   {
  349.     if(this.m_queryObject != null)
  350.     {
  351.       this.m_queryObject.ResetQuery();
  352.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + this.m_strName + "\" WHERE url = \"" + mediaURL + "\"");
  353.       
  354.       this.m_queryObject.Execute();
  355.       this.m_queryObject.WaitForCompletion();
  356.       
  357.       var resObj = this.m_queryObject.GetResultObject();
  358.  
  359.       if(resObj.GetRowCount())
  360.         return resObj.GetRowCell(0, 0);    
  361.     }
  362.  
  363.     return "";
  364.   },
  365.  
  366.   GetColumnValuesByIndex: function(mediaIndex, nColumnCount, aColumns, nValueCount)
  367.   {
  368.     nValueCount = 0;
  369.     var aValues = new Array();
  370.  
  371.     if(this.m_queryObject != null)
  372.     {
  373.       var strQuery = "SELECT ";
  374.       this.m_queryObject.ResetQuery();
  375.       
  376.       var i = 0;
  377.       for( ; i < nColumnCount; i++)
  378.       {
  379.         strQuery += aColumns[i];
  380.         
  381.         if(i < nColumnCount - 1)
  382.           strQuery += ", ";
  383.       }
  384.       
  385.       strQuery += " FROM " + this.m_strName + " WHERE id = \"" + mediaIndex + "\"";
  386.       this.m_queryObject.AddQuery(strQuery);
  387.       
  388.       this.m_queryObject.Execute();
  389.       this.m_queryObject.WaitForCompletion();
  390.       
  391.       var resObj = this.m_queryObject.GetResultObject();
  392.       nValueCount = resObj.GetColumnCount();
  393.       
  394.       for(var i = 0; i < nValueCount; i++)
  395.       {
  396.         aValues.push(resObj.GetRowCell(0, i));
  397.       }    
  398.     }
  399.     
  400.     return aValues;
  401.   },
  402.   
  403.   GetColumnValuesByURL: function(mediaURL, nColumnCount, aColumns, nValueCount)
  404.   {
  405.     nValueCount = 0;
  406.     var aValues = new Array();
  407.  
  408.     if(this.m_queryObject != null)
  409.     {
  410.       var strQuery = "SELECT ";
  411.       this.m_queryObject.ResetQuery();
  412.       
  413.       var i = 0;
  414.       for( ; i < nColumnCount; i++)
  415.       {
  416.         strQuery += aColumns[i];
  417.         
  418.         if(i < nColumnCount - 1)
  419.           strQuery += ", ";
  420.       }
  421.       
  422.       strQuery += " FROM \"" + this.m_strName + "\" WHERE url = \"" + mediaURL + "\"";
  423.       this.m_queryObject.AddQuery(strQuery);
  424.       
  425.       this.m_queryObject.Execute();
  426.       this.m_queryObject.WaitForCompletion();
  427.       
  428.       var resObj = this.m_queryObject.GetResultObject();
  429.       nValueCount = resObj.GetColumnCount();
  430.       
  431.       for(var i = 0; i < nValueCount; i++)
  432.       {
  433.         aValues.push(resObj.GetRowCell(0, i));
  434.       }    
  435.     }
  436.     
  437.     return aValues;
  438.   },
  439.   
  440.   SetColumnValueByIndex: function(mediaIndex, strColumn, strValue, bWillRunLater)
  441.   {
  442.     if(this.m_queryObject != null)
  443.     {
  444.       if(!bWillRunLater)
  445.         this.m_queryObject.ResetQuery();
  446.       
  447.       strValue = strValue.replace(/"/g, "\"\"");
  448.       this.m_queryObject.AddQuery("UPDATE \"" + this.m_strName + "\" SET " + strField + " = \"" + strValue + "\" WHERE id = \"" + mediaIndex + "\"");
  449.       
  450.       if(!bWillRunLater)
  451.       {
  452.         this.m_queryObject.Execute();
  453.         this.m_queryObject.WaitForCompletion();
  454.       }
  455.     }
  456.     
  457.     return;
  458.   },
  459.   
  460.   SetColumnValueByURL: function(mediaURL, strColumn, strValue, bWillRunLater)
  461.   {
  462.     if(this.m_queryObject != null)
  463.     {
  464.       if(!bWillRunLater)
  465.         this.m_queryObject.ResetQuery();
  466.       
  467.       strValue = strValue.replace(/"/g, "\"\"");
  468.       this.m_queryObject.AddQuery("UPDATE \"" + this.m_strName + "\" SET " + strColumn + " = \"" + strValue + "\" WHERE url = \"" + mediaURL + "\"");
  469.       
  470.       if(!bWillRunLater)
  471.       {
  472.         this.m_queryObject.Execute();
  473.         this.m_queryObject.WaitForCompletion();
  474.       }
  475.     }
  476.     
  477.     return;
  478.   },
  479.  
  480.   SetColumnValuesByIndex: function(mediaIndex, nColumnCount, aColumns, nValueCount, aValues, bWillRunLater)
  481.   {
  482.     if(this.m_queryObject != null ||
  483.        nColumnCount != nValueCount)
  484.     {
  485.       if(!bWillRunLater)
  486.         this.m_queryObject.ResetQuery();
  487.       
  488.       var strQuery = "UPDATE " + this.m_strName + " SET ";
  489.       var i = 0;
  490.       for(; i < nColumnCount; i++)
  491.       {
  492.         aValues[i] = aValues[i].replace(/"/g, "\"\"");
  493.         strQuery += aColumns[i] + " = \"" + aValues[i] + "\"";
  494.         if(i < nColumnCount - 1)
  495.           strQuery += ", ";
  496.       }
  497.       
  498.       strQuery += " WHERE id = \"" + mediaIndex + "\"";
  499.       this.m_queryObject.AddQuery(strQuery);
  500.       
  501.       if(!bWillRunLater)
  502.       {
  503.         this.m_queryObject.Execute();
  504.         this.m_queryObject.WaitForCompletion();
  505.       }
  506.     }
  507.  
  508.     return;
  509.   },
  510.   
  511.   SetColumnValuesByURL: function(mediaURL, nColumnCount, aColumns, nValueCount, aValues, bWillRunLater)
  512.   {
  513.     if(this.m_queryObject != null ||
  514.        nColumnCount != nValueCount)
  515.     {
  516.       if(!bWillRunLater)
  517.         this.m_queryObject.ResetQuery();
  518.       
  519.       var strQuery = "UPDATE \"" + this.m_strName + "\" SET ";
  520.       var i = 0;
  521.       for(; i < nColumnCount; i++)
  522.       {
  523.         aValues[i] = aValues[i].replace(/"/g, "\"\"");
  524.         strQuery += aColumns[i] + " = \"" + aValues[i] + "\"";
  525.         if(i < nColumnCount - 1)
  526.           strQuery += ", ";
  527.       }
  528.       
  529.       strQuery += " WHERE url = \"" + mediaURL + "\"";
  530.       this.m_queryObject.AddQuery(strQuery);
  531.       
  532.       if(!bWillRunLater)
  533.       {
  534.         this.m_queryObject.Execute();
  535.         this.m_queryObject.WaitForCompletion();
  536.       }
  537.     }
  538.  
  539.     return;
  540.   },
  541.  
  542.   SetName: function(strName)
  543.   {
  544.     if(strName != "")
  545.       this.m_strName = strName;
  546.  
  547.     return;
  548.   },
  549.   
  550.   GetName: function()
  551.   {
  552.     return this.m_strName;
  553.   },
  554.   
  555.   SetReadableName: function(strReadableName)
  556.   {
  557.     if(this.m_queryObject != null)
  558.     {
  559.       this.m_queryObject.ResetQuery();
  560.       
  561.       strReadableName = strReadableName.replace(/"/g, "\"\"");
  562.       this.m_queryObject.AddQuery("UPDATE " + SIMPLEPLAYLIST_LIST_TABLE_NAME + " SET readable_name = \"" + strReadableName + "\" WHERE name = \"" + this.m_strName + "\"");
  563.       
  564.       this.m_queryObject.Execute();
  565.       this.m_queryObject.WaitForCompletion();
  566.     }
  567.         
  568.     return;
  569.   },
  570.   
  571.   GetReadableName: function()
  572.   {
  573.     var strReadableName = "";
  574.     this.m_queryObject.ResetQuery();
  575.     this.m_queryObject.AddQuery("SELECT readable_name FROM " + SIMPLEPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + this.m_strName + "\"");
  576.     
  577.     this.m_queryObject.Execute();
  578.     this.m_queryObject.WaitForCompletion();
  579.     
  580.     var resObj = this.m_queryObject.GetResultObject();
  581.     
  582.     if(resObj.GetRowCount())
  583.       strReadableName = resObj.GetRowCell(0, 0);    
  584.     
  585.     return strReadableName;
  586.   },
  587.  
  588.   QueryInterface: function(iid)
  589.   {
  590.       if (!iid.equals(Components.interfaces.nsISupports) &&
  591.           !iid.equals(SONGBIRD_SIMPLEPLAYLIST_IID))
  592.           throw Components.results.NS_ERROR_NO_INTERFACE;
  593.       return this;
  594.   }
  595. };
  596.  
  597. /**
  598.  * \class sbSimplePlaylistModule
  599.  * \brief 
  600.  */
  601. var sbSimplePlaylistModule = 
  602. {
  603.   registerSelf: function(compMgr, fileSpec, location, type)
  604.   {
  605.       compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  606.       compMgr.registerFactoryLocation(SONGBIRD_SIMPLEPLAYLIST_CID, 
  607.                                       SONGBIRD_SIMPLEPLAYLIST_CLASSNAME, 
  608.                                       SONGBIRD_SIMPLEPLAYLIST_CONTRACTID, 
  609.                                       fileSpec, 
  610.                                       location,
  611.                                       type);
  612.   },
  613.  
  614.   getClassObject: function(compMgr, cid, iid) 
  615.   {
  616.       if (!cid.equals(SONGBIRD_SIMPLEPLAYLIST_CID))
  617.           throw Components.results.NS_ERROR_NO_INTERFACE;
  618.  
  619.       if (!iid.equals(Components.interfaces.nsIFactory))
  620.           throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  621.  
  622.       return sbSimplePlaylistFactory;
  623.   },
  624.  
  625.   canUnload: function(compMgr)
  626.   { 
  627.     return true; 
  628.   }
  629. }; //sbSimplePlaylistModule
  630.  
  631. /**
  632.  * \class sbSimplePlaylistFactory
  633.  * \brief 
  634.  */
  635. var sbSimplePlaylistFactory =
  636. {
  637.     createInstance: function(outer, iid)
  638.     {
  639.         if (outer != null)
  640.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  641.     
  642.         if (!iid.equals(SONGBIRD_SIMPLEPLAYLIST_IID) &&
  643.             !iid.equals(Components.interfaces.nsISupports))
  644.             throw Components.results.NS_ERROR_INVALID_ARG;
  645.  
  646.         return (new CSimplePlaylist()).QueryInterface(iid);
  647.     }
  648. }; //sbSimplePlaylistFactory
  649.  
  650. /**
  651.  * \function NSGetModule
  652.  * \brief 
  653.  */
  654. function NSGetModule(comMgr, fileSpec)
  655.   return sbSimplePlaylistModule;
  656. } //NSGetModule